package org.xxx.epms.models.epms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxx.epms.models.epms.dto.AqiFeedbackParam;
import org.xxx.epms.models.epms.mapper.MonitorMapper;
import org.xxx.epms.models.epms.model.AqiFeedback;
import org.xxx.epms.models.epms.mapper.AqiFeedbackMapper;
import org.xxx.epms.models.epms.model.AqiMeasuredResult;
import org.xxx.epms.models.epms.model.Monitor;
import org.xxx.epms.models.epms.service.AqiFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.xxx.epms.models.epms.service.AqiMeasuredResultService;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lb
 * @since 2024-06-19
 */
@Service
public class AqiFeedbackServiceImpl extends ServiceImpl<AqiFeedbackMapper, AqiFeedback> implements AqiFeedbackService {

    @Autowired
    private AqiFeedbackMapper aqiFeedbackMapper;
    @Autowired
    private MonitorMapper monitorMapper;
    @Autowired
    private AqiMeasuredResultService aqiMeasuredResultService;

    private QueryWrapper<AqiFeedback> getQueryWrapper(AqiFeedbackParam aqiFeedbackParam){
        QueryWrapper<AqiFeedback> wrapper = new QueryWrapper<>();
        if(aqiFeedbackParam.getFbId()!= null){
            wrapper.lambda().eq(AqiFeedback::getFbId, aqiFeedbackParam.getFbId());
        }
        if(aqiFeedbackParam.getFbAqiLevel() != null){
            wrapper.lambda().eq(AqiFeedback::getFbAqiLevel, aqiFeedbackParam.getFbAqiLevel());
        }
        if(aqiFeedbackParam.getCityId() != null){
            wrapper.lambda().eq(AqiFeedback::getCityId, aqiFeedbackParam.getCityId());
        }
        if(aqiFeedbackParam.getProvinceId() != null) {
            wrapper.lambda().eq(AqiFeedback::getProvinceId, aqiFeedbackParam.getProvinceId());
        }
        if(aqiFeedbackParam.getFbAssignState() != null){
            wrapper.lambda().eq(AqiFeedback::getFbAssignState, aqiFeedbackParam.getFbAssignState());
        }
        if(aqiFeedbackParam.getMonTel() != null){
            QueryWrapper<Monitor> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Monitor::getMonTel, aqiFeedbackParam.getMonTel());
            Monitor monitor = monitorMapper.selectOne(queryWrapper);
            if(monitor != null){
                wrapper.lambda().eq(AqiFeedback::getMonId, monitor.getMonId());
            }else{
                wrapper.lambda().eq(AqiFeedback::getMonId, 0);
            }
        }
        if(aqiFeedbackParam.getFbStartTime() != null && aqiFeedbackParam.getFbEndTime() != null){
            wrapper.lambda().between(AqiFeedback::getFbTime, aqiFeedbackParam.getFbStartTime(), aqiFeedbackParam.getFbEndTime());
        }
        if(aqiFeedbackParam.getAssignStartTime() != null && aqiFeedbackParam.getAssignEndTime() != null){
            wrapper.lambda().between(AqiFeedback::getFbAssignTime, aqiFeedbackParam.getAssignStartTime(), aqiFeedbackParam.getAssignEndTime());
        }
        return wrapper;
    }

    @Override
    public int getTotalPage(AqiFeedbackParam aqiFeedbackParam, int pageSize) {
        if(aqiFeedbackParam == null){
            long total = aqiFeedbackMapper.selectCount(null);
            return (int)total / pageSize + (total % pageSize > 0? 1 : 0);
        }else{
            QueryWrapper<AqiFeedback> wrapper = getQueryWrapper(aqiFeedbackParam);
            long total = aqiFeedbackMapper.selectCount(wrapper);
            return (int)total / pageSize + (total % pageSize > 0 ? 1 : 0);
        }
    }

    @Override
    public Page<AqiFeedback> getPageList(AqiFeedbackParam aqiFeedbackParam, int pageSize, int pageNum) {
       if(aqiFeedbackParam == null){
            Page<AqiFeedback> page = new Page<>(pageNum, pageSize);
            return page(page, null);
       }else{
           QueryWrapper<AqiFeedback> wrapper = getQueryWrapper(aqiFeedbackParam);
           Page<AqiFeedback> page = new Page<>(pageNum, pageSize);
           System.out.println(aqiFeedbackParam);
           return page(page, wrapper);
       }
    }

    @Override
    public int assignAqiFeedback(Integer fbId, Integer insId) {
        AqiFeedback aqiFeedback = aqiFeedbackMapper.selectById(fbId);
        //更新指派状态为已指派
        aqiFeedback.setFbAssignState(1);
        //获取当前时间
        Date date = new Date();
        //设置指派时间为当前时间
        aqiFeedback.setFbAssignTime(date);

        AqiMeasuredResult aqiMeasuredResult = aqiMeasuredResultService.getMeasuredResultByFbId(fbId)!=null ?
                aqiMeasuredResultService.getMeasuredResultByFbId(fbId).getAqiMeasuredResult()
                : new AqiMeasuredResult();
        aqiMeasuredResult.setFbId(fbId);
        aqiMeasuredResult.setInsId(insId);
        aqiMeasuredResult.setProvinceId(aqiFeedback.getProvinceId());
        aqiMeasuredResult.setCityId(aqiFeedback.getCityId());
        aqiMeasuredResult.setAqimrAddress(aqiFeedback.getFbAddress());
        //已指派未检测状态设为0
        aqiMeasuredResult.setAqimrState(0);
        aqiMeasuredResultService.createAqiMeasuredResult(aqiMeasuredResult);
        System.out.println("指派反馈成功"+fbId+"       "+insId);
        return aqiFeedbackMapper.updateById(aqiFeedback);
    }


}
